<?php
namespace app\admin\controller;

use think\Request;

class Index extends Base
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function index()
    {
        $id = $this->uid;

        $data_user = model('admin_user')->field('id,uname,pen_name,email')->find($id);

        $data_login = model('LoginLog')->where('uid', $id)->order('id desc')->limit(10)->select();

        $this->assign('data_user', $data_user);
        $this->assign('data_login', $data_login);
        return $this->fetch();
    }

    /**
     * 修改邮箱 or 笔名
     */
    public function modify($value, $field)
    {
        if (Request::instance()->isAjax()) {
            $result = model('admin_user')->save([$field=>$value], ['id'=>$this->uid]);

            if ($result !== false) {
                return ['code'=>2, 'msg'=>config('msg.1003'), $field=>$value];
            } else {
                return ['code'=>0, 'msg'=>config('msg.1004')];
            }
        }
    }
}
